package com.woniuxy.services.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.daos.TbOrderDao;
import com.woniuxy.daos.TbSectionDao;
import com.woniuxy.entitys.TbMalfunction;
import com.woniuxy.entitys.TbOrder;
import com.woniuxy.entitys.TbSection;
import com.woniuxy.services.OrderListService;
import com.woniuxy.utils.JWTUtils;
import com.woniuxy.utils.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Classname OrderListServiceImpl
 * @Date 2024/1/10 15:22
 */
@Service
public class OrderListServiceImpl implements OrderListService {
    @Autowired
    private JWTUtils jwtUtils;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TbOrderDao tbOrderDao;
    @Autowired
    private TbSectionDao tbSectionDao;
    @Override
    public PageInfo<TbOrder> selAllOrderList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbOrder> tbOrders = tbOrderDao.selectOrderList();
        PageInfo<TbOrder> pageInfo = new PageInfo<>(tbOrders);
        return pageInfo;
    }

    @Override
    public PageInfo<TbOrder> selOrderByHospital(Integer pageNum, Integer pageSize, Integer key1) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbOrder> tbOrders= tbOrderDao.getOrderByHospital(key1);
        PageInfo<TbOrder> pageInfo = new PageInfo<>(tbOrders);
        return pageInfo;
    }

    @Override
    public PageInfo<TbOrder> selOrderByDepartment(Integer pageNum, Integer pageSize, Integer key2) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbOrder> tbOrders= tbOrderDao.getOrderByDepartment(key2);
        PageInfo<TbOrder> pageInfo = new PageInfo<>(tbOrders);
        return pageInfo;
    }

    @Override
    public int updateOrderSoftdel(Integer oid) {
        int i = tbOrderDao.updateSoftdel(oid);
        return i;
    }

    @Override
    public PageInfo<TbOrder> getOrderBySectionSome(String token,int pageNum, int pageSize,String onum, String bnum, String cname, Integer orderstatus) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String username = jwtUtils.getUsername(token);
        LoginUser o = (LoginUser) valueOperations.get("user:" + username);
        TbSection section = tbSectionDao.getSectionInfoByUid(o.getUser().getUid());
        Integer did=section.getDid();
        PageHelper.startPage(pageNum,pageSize);
        List<TbOrder> orderBySectionSome = tbOrderDao.getOrderBySectionSome(did,onum, bnum, cname, orderstatus);
        PageInfo<TbOrder> pageInfo = new PageInfo<>(orderBySectionSome);
        return pageInfo;
    }

    @Override
    public int getAllactualpay() {
        int allactualpay = tbOrderDao.getAllactualpay();
        return allactualpay;
    }

    @Override
    public List<TbOrder> getAllByCascade(Integer pageNum, Integer pageSize, List<Integer> cascadeValue) {
        PageHelper.startPage(pageNum, pageSize);
        return tbOrderDao.getAllOrderByCascade(cascadeValue.get(0), cascadeValue.get(1));
    }

    //计算时间差
//    @Override
//    public String getDate(int oid) throws ParseException {
//        TbOrder tbOrder = tbOrderDao.selectByPrimaryKey(oid);
//        Date orderTime = tbOrder.getOrderTime();
//        Date returnTime = tbOrder.getReturnTime();
//        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        long time = sdf1.parse(String.valueOf(orderTime)).getTime();
//        long time2 = sdf1.parse(String.valueOf(returnTime)).getTime();
//        long l = (time2 - time)/1000;
//        int h = (int) (l/3600);
//        int m = (int) (l%3600)/60;
//        int s = (int) (l%60);
//        String str = new String(h+"时"+m+"分"+s+"秒");
//
//        return str;
//    }
}
